<?php
/**
 * This file is part of Hyperf Mall.
 *
 * @link     https://gitee.com/scwlkj/hypref_mall
 * contact  hyperf_mall@163.com
 */
namespace App\Repository;

use App\Constants\Message;
use App\Constants\PointCode;
use App\Model\MessageModel;
use Hyperf\Di\Annotation\Inject;

class MessageRepository extends BaseRepository
{
    /**
     * @Inject
     * @var MessageModel
     */
    protected $model;

    public function orderContent($oid, $type): string
    {
        $content = '';
        switch ($type) {
            case Message::MS_CREATE_ORDER:
                $content = "您的订单{$oid}尚未付款，快来付款吧，不然宝贝就被抢走啦~";
                break;
            case Message::MS_ORDER_CANCEL:
                $content = "您的订单 {$oid} 已取消，请您重新进行购买。";
                break;
            // 金额付款和积分付款类型的付款消息合并成一个
            case Message::MS_ORDER_PAY:
                $content = "您的订单{$oid}已经付款成功，请注意签收";
                break;
            case Message::MS_ORDER_DELIVERY:
                $content = "您的订单 {$oid} 已经发货，请注意物流信息及时签收。";
                break;
            case Message::MS_ORDER_CONFIRM:
                $content = "您的订单{$oid}尚未评价，期待您的好评！";
                break;
            case Message::MS_ORDER_EVALUATE:
                $content = "您的订单 {$oid} 已评价，谢谢您的评价，祝您购物愉快。";
                break;
            case Message::MS_ORDER_FINISH:
                $content = "您的订单 {$oid} 已完成，感谢你的支持，祝您购物愉快。";
                break;
            case Message::MS_ORDER_REFUND:
                $content = "您的订单 {$oid} 已经申请退款，请等待商家审核。";
                break;
            case Message::MS_ORDER_REFUND_PASS:
                $content = "您的订单 {$oid} 申请退款审核通过，请及时处理。";
                break;
            case Message::MS_ORDER_REFUND_REJECT:
                $content = "您的订单 {$oid} 申请退款被驳回，您可以重新联系商家进行处理。";
                break;
            case Message::MS_ORDER_REFUND_CANCEL:
                $content = "您的订单 {$oid} 退款申请已取消，感谢您的理解，祝您购物愉快。";
                break;
            case Message::MS_ORDER_REFUND_FINISH:
                $content = "您的订单 {$oid} 退款完成，感谢您的支持。";
                break;
        }

        return $content;
    }

    public function pointContent($type, $point_type, $point): string
    {
        $content = '';
        switch ($type) {
            case Message::MS_POINT_ADD:
            case Message::MS_POINT_DESC:
                $content = "变动类型:" . PointCode::$type_msg[$point_type]."  +" . $point;
                break;
        }
        return $content;
    }

    /**
     * Notes: 订单类 站内信消息通知
     * Author: Bruce.z
     * DateTime: 2022/7/20 10:33
     * @param $oid
     * @param int $member_id
     * @param int $type
     */
    public function orders($oid, int $member_id, int $type):void
    {
        $data = array(
            'msg_type' => $type,
            'oid' => $oid,
            'member_id' => $member_id,
            'sender_type' => Message::SEND_TYPE_ORDER,
            'title' => Message::$type_msg[$type],
            'content' => $this->orderContent($oid, $type),
        );

        $this->baseAdd($data);
    }

    /**
     * Notes: 积分变动消息提醒
     * Author: Bruce.z
     * DateTime: 2022/7/20 11:47
     * @param int $member_id
     * @param $type
     * @param $point_type
     * @param $point
     */
    public function point(int $member_id, $type, $point_type, $point):void
    {
        $data = array(
            'msg_type' => $type,
            'member_id' => $member_id,
            'sender_type' => Message::SEND_TYPE_SYSTEM,
            'title' => Message::$type_msg[$type],
            'content' => $this->pointContent($type, $point_type, $point),
        );

        $this->baseAdd($data);
    }

    /**
     * Notes: 标记已读
     * Author: Bruce.z
     * DateTime: 2022/7/20 12:00
     * @param int $member_id
     * @return bool
     */
    public function messageRead(int $member_id): bool
    {
        return $this->updateByConditions(['member_id'=>$member_id], ['status'=>Message::MS_READ]);
    }
}
